/**
 *
 * @Author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. 3 as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

import java.util.*;

// Generic package translator.  PackageTranslators created in run time by JavaType represents the hierarchy of packages registered java types.
class PackageTranslator implements ITranslator {

   protected String                             name   = null;
   protected LinkedHashMap<String, Translation> fields = new LinkedHashMap<String, Translation>();

   public PackageTranslator( String name ) {
      this.name = name;
   }

   public Translation call(String id, Translation ... args) throws TranslatorException {
      throw new TranslatorException( "Packages do not contain methods!  Error during translation of "+ name() +"."+ id +" invocation for "+ TranslatorUtil.call_description(args) );
   }

   public Translation field(String id) throws TranslatorException {
      Translation tn=fields.get(id);
      if(tn==null) {
         throw new TranslatorException( "Unknown field "+ name() +"."+ id);
      }
      return tn;
   }

   public String name() {
      return name;
   }

   public String php_name() throws TranslatorException {
      throw new TranslatorException( "Packages names are not translated to php!  Error during translation of "+ name() );
   }

   public ITranslator get_field_translator( String id ) {
      Translation t = fields.get( id );
      if( t == null ) {
         return null;
      }
      return t.type.translator();
   }

   public void add_field( String id, JavaType jt ) {
      Translation translation = fields.get( id );
      if( translation != null ) {
         throw new Error( "Field "+ id +" is already assigned to type "+ ( translation.type != null ? translation.type.name() : " null " ) +" in package "+ name +"!" );
      }
      Set<String> empty_set = Collections.emptySet();
      translation  = new Translation( null, jt, false, false, empty_set );
      fields.put( id, translation );
   }
}
